package com.xhs.hawaii;

import com.xhs.hawaii.framework.Env;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

import static springfox.documentation.builders.PathSelectors.regex;

/**
 * @author tangchuang
 * @version 1.0
 * @since 2018/1/20
 */
@SpringBootApplication
@EnableSwagger2
@Slf4j
public class HawaiiApplication {

    //环境参数
    private static final String evn_ex = "--spring.profiles.active=";

    public static void main(String[] args) {
        log.info("===============start to init================");
        String env = Env.LOCAL.name();
        if (args != null && args.length > 0) {
            for (String arg : args) {
                if (null != arg && arg.startsWith(evn_ex)) {
                    env = arg.substring(evn_ex.length());
                }
            }
        }
        log.info("====== Now env is " + env + " =======");
        System.setProperty("user.env", env.toLowerCase());
        System.setProperty("sun.net.client.defaultConnectTimeout", "2000");
        System.setProperty("sun.net.client.defaultReadTimeout", "1500");
        System.setProperty("user.timezone", "GMT+8");
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
        SpringApplication.run(HawaiiApplication.class, args);
    }

    @Bean
    public Docket api() {
        List<Parameter> pars = new ArrayList<Parameter>();
        ParameterBuilder Authorization = new ParameterBuilder();
        Authorization.name("Authorization").description("用户token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(Authorization.build());
        if ("prod".equals(System.getProperty("user.env"))) {
            return new Docket(DocumentationType.SWAGGER_2)
                    .groupName("hawaii").apiInfo(metadata()).select().paths(PathSelectors.none())
                    .apis(RequestHandlerSelectors.basePackage("com.xhs.hawaii")).build();

        } else {
            return new Docket(DocumentationType.SWAGGER_2)
                    .groupName("hawaii").apiInfo(metadata()).select().paths(regex("/*.*"))
                    .apis(RequestHandlerSelectors.basePackage("com.xhs.hawaii")).build().globalOperationParameters(pars);
        }

    }

    private ApiInfo metadata() {
        return new ApiInfoBuilder().title("小红书自营结算").description("接口url= host:port/hawaii/${path}").version("1.0").build();
    }
}
